<?php
$vocName = 'Category';
$voc = $this->vocabulary;
if (is_object($voc))
{
    $vocName = $voc->name;
}

/*$this->breadcrumbs = array();
if ($this->module->id === 'Admin')
    $this->breadcrumbs['Categorization'] = array('/Admin/vocabulary/index');
else
    $this->breadcrumbs[$this->module->id] = array('/'.$this->module->id);
$this->breadcrumbs[$vocName] = array('/'.$this->module->id.'/'.$this->id);
$this->breadcrumbs[] = 'Manage';*/

$this->breadcrumbs=array(
    'Categorization'=>array('/Admin/vocabulary/index'),
    'Category'=>array('index'),
    'Manage',
);

$cs = Yii::app()->clientScript;
$cs->registerCoreScript('bbq');
$cs->registerScript(__CLASS__.'#ChangeType', "jQuery('.category-form select').change(function(){
    if ($(this).attr('id')=='module')
        $('#type').val('');
    jQuery('.category-form').submit();
});");

$cs->registerScriptFile(Yii::app()->theme->baseUrl.'/scripts/fancybox/jquery.fancybox-1.3.4.pack.js', CClientScript::POS_END);
$cs->registerCssFile(Yii::app()->theme->baseUrl.'/scripts/fancybox/jquery.fancybox-1.3.4.css');
/*$cs->registerScript(__CLASS__.'CreateVocabulary', "
jQuery('a.create-type').fancybox({
    width: 500,
    height: 330,
    type: 'iframe',
    onClosed: function (){
        window.location.reload();
    }
});
");*/

$cs->registerScript(__CLASS__.'CreateTerm', "
if (jQuery('#type').val() === '')
    jQuery('.create-term').hide();
else
    jQuery('.create-term').show();
jQuery('.create-term, .actions-column a').click(function(){
    var href = jQuery.param.querystring(jQuery(this).attr('href'), {v:jQuery('#type').val(),m:jQuery('#module').val()});
    jQuery(this).attr('href', href);
});
");
/*$cs->registerScript(__CLASS__.'CreateCategory', "
jQuery('a.create-term').fancybox({
    width: 500,
    height: 330,
    type: 'iframe',
    model: true,
    onClosed: function (){
        window.location.reload();
    }
});
");*/
if ($this->get('is_admin', 1) == 0)
{
    $cs->registerScript(__CLASS__.'HideTypeInfo', "jQuery('.type-info').hide();");
}
?>

<h1>Manage <?php echo $vocName;?></h1>

<?php if ($this->module->id === 'Admin'):?>
<?php echo CHtml::beginForm(array('/Admin/term/index'), 'get', array('class'=>'category-form'));?>
    <div class="row type-info">
        <?php echo CHtml::link('Manage type', array('/Admin/vocabulary/admin'));?>&nbsp;&nbsp;&nbsp;
        <?php echo CHtml::link('Create new type', array('/Admin/vocabulary/create','module'=>$module), array('class'=>'create-type'));?>
    </div>
    <div class="row" style="margin-top: 10px;">
        <span class="type-info">
        <label>Module</label>
        <?php echo CHtml::dropDownList('module', $module, $modules, array('prompt'=>'system'));?>
        <label>Type</label>
        <?php echo CHtml::dropDownList('type', $type, $types, array('prompt'=>'---Select---'));?>
        </span>
        <?php echo CHtml::link('Create new Category', array('create'), array('class'=>'create-term', 'style'=>'display:none'));?>&nbsp;&nbsp;&nbsp;
        <?php echo CHtml::link(' Bulk Insert New Categories', array('bulkCreate'), array('class'=>'create-term', 'style'=>'display:none'));?>
    </div>
</form>
<?php endif;?>

<?php $this->widget('NestedTaxonomy', array(
    'module'=>$module,
    'taxonomy'=>$type,
    'state'=>'',
    'updateButtonUrl'=>"Yii::app()->controller->createUrl('update', array('id'=>\$data['id']))",
    'deleteButtonUrl'=>"Yii::app()->controller->createUrl('delete', array('id'=>\$data['id']))",
    'statusButtonUrl'=>"Yii::app()->controller->createUrl('status', array('id'=>\$data['id']))",
));
$this->menu[] = array('label' => 'Delete selected items', 'url'=>$this->createUrl('delete'), 'linkOptions' => array('class'=>'delete-all'));
if ($this->module->id !== 'Admin')
    $this->menu[] = array('label' => 'Create New', 'url'=>$this->createUrl('create'));
$script = "
$('body').delegate('.delete-all', 'click', function(){
    var parent = null, same_parent = true, data = [];
    $.each($('.checkbox-column input:checked'), function(){
        if (parent == null)
            parent = $(this).closest('ul');
        else
        {
            if (parent.get(0) != $(this).closest('ul').get(0))
            {
                same_parent = false;
                return false;
            }
        }
        data.push($(this).val());
    });
    if (same_parent == false)
    {
        alert('Cannot delete items those are not belong to the same parent');
        return false;
    }
    if (!confirm('Are you sure you want to delete this items?'))
        return false;
    jQuery.get(jQuery(this).attr('href'), {id: data.join(',')}, function(res){
        window.location = window.location.href.replace(/\#.*/, '');
    });
    return false;
});
";
Yii::app()->clientScript->registerScript('BulkDelete', $script);
?>
